<?php
/**
 * the include filter adds the <dig:include /> tag to the page
 *
 * this tag incudes another script
 * you have two options:
 *
 * if you set the src attribute the filter will map the src to an item in the tree
 *
 * if you set the module, controller, and action attributes the filter will run the view action helper
 * which is passes all of the include tag's attributes as well as the current item
 *
 */
class DigCms_View_Filter_Children extends DigCms_View_Filter_ContentFilter
{
    public $tag = 'children';

    protected function _callback($matches)
    {
        $attr = $this->getAttributes($matches[0]);
        if (is_array($attr)) {
            $type = isset($attr['type']) ? $attr['type'] : null;
            $module = isset($attr['module']) ? $attr['module'] : 'digCms';
            $children = $this->view->item->children($type);
            $response = array();
            $params = $attr;
            if($children) {
                foreach ($children as $child) {
                    $params['item'] = $child;
                    $response[] = $this->view->partial($attr['partial'], $module, $params);
                }
            }
            return implode(PHP_EOL, $response);
        }
        return null;
    }

}
?>
